package com.example.cs_jump.Util;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ParsingUrl {
    private Document document;
    private Element element;
    private Elements elements;
    private String[] imageurl;
    String url = "https://www.csgowiki.top/utility/";

    public ParsingUrl(String id) throws IOException {
        this.document = Jsoup.connect(url + id).get();
    }

    //    //标题
//    public String getTitle() {
//        elements = document.select("title");
//        return elements.html();
//    }
    public String[] getImageurl() {
        return imageurl;
    }

    public void setImageurl(String[] imageurl) {
        this.imageurl = imageurl;
    }

    //内容+图片的过滤
    public String infoImage() {
        element = document.getElementById("markdown");
        String str = element.html();
        return filter(str);
    }

    //过滤[] image 等除中文符号
    public String filter(String str) {
        String regEx = "[\\[\\]\\!]+";
        String oks = "";
        Pattern pt = Pattern.compile(regEx);
        Matcher matcher = pt.matcher(str);
        if (matcher.find()) {
            oks = matcher.replaceAll("");
        }
        //filterChinese(oks);
        return filterUrl(oks);
    }


    //过滤url
    public String filterUrl(String str) {
        return str.replaceAll("[a-zA-Z0-9\\:\\/\\.\\(\\)]+", "");
    }

    //只保留url
    public void filterChinese(String str) {
        String str1 = str.replaceAll("[\\u4e00-\\u9fa5\\,\\,\\,\\， \\：,\\ （）,\\*,\\。，。。]", "").trim();
        // String str1=str.replaceAll("([hH][tT]{2}[pP]://|[hH][tT]{2}[pP][sS]://|[wW]{3}.|[wW][aA][pP].|[fF][tT][pP].|[fF][iI][lL][eE].)[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]","").trim();
        setImageurl(saveUrl(str1.trim()));
    }

    //过滤除url以外其他符号
    public String[] saveUrl(String result) {
        String s1 = result.replaceAll("[\\)A-Z]", "").trim();
//        String s2=s1.replaceAll("[A-Z]","").trim();
        String[] s = s1.split("image\\(|cover\\(+");
        //String[]s=s1.split("//");
        for (int i=0;i<s.length;i++){
            s[i].replaceAll("([hH][tT]{2}[pP]://|[hH][tT]{2}[pP][sS]://|[wW]{3}.|[wW][aA][pP].|[fF][tT][pP].|[fF][iI][lL][eE].)[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]","");
        }
        return replaceNull(s);
    }

    public String[] replaceNull(String[] str) {
        //用StringBuffer来存放数组中的非空元素，用“;”分隔
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < str.length; i++) {
            if ("".equals(str[i])) {
                continue;
            }

            sb.append(str[i]);
            if (i != str.length - 1) {
                sb.append(";");
            }
        }
        //用String的split方法分割，得到数组
        str = sb.toString().split(";");
        for (int i = 0;i<str.length;i++){
            str[i].replaceAll("([hH][tT]{2}[pP]://|[hH][tT]{2}[pP][sS]://|[wW]{3}.|[wW][aA][pP].|[fF][tT][pP].|[fF][iI][lL][eE].)[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]","");
            return str;
        }
        return null;
    }
}
